


from rest_framework import serializers
from goods.models import SKU,SKUSpecification,GoodsCategory,SPU


class SKUSpecSimpleSerializer(serializers.ModelSerializer):
    spec_id = serializers.IntegerField()
    option_id = serializers.IntegerField()
    class Meta:
        model = SKUSpecification
        fields = ['spec_id', 'option_id']


class SKUModelSerializer(serializers.ModelSerializer):
    spu = serializers.StringRelatedField()
    spu_id = serializers.IntegerField()
    category = serializers.StringRelatedField()
    category_id = serializers.IntegerField()

    # 代表就是与当前sku主表对象关联的多有从表（SKUSpecification）对象集（多个）
    specs = SKUSpecSimpleSerializer(many=True)

    class Meta:
        model = SKU
        fields = ["id","name","caption","spu","spu_id","category","category_id","price","cost_price","market_price","stock","sales","comments","is_launched", "default_image","specs"]


class SKUCategorySimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsCategory
        fields = ['id', 'name']


class SPUSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = SPU
        fields =["id", "name"]












